﻿using HelloOne.NewsSvc.Models;
using HelloOne.NewsSvc.Services;
using HelloOne.NewsSvc.ViewModels;
using HelloOne.Repositories;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;

namespace HelloOne.NewsSvc.Controllers
{
    /// <summary>
    /// 资讯类别
    /// </summary>
    [Produces("application/json")]
    [Route("api/Category")]
    public class CategoryController : Controller
    {
        private IRepository<Category> _repository;
        public CategoryController(IRepositoryProvider provider)
        {
            _repository = provider.Create<Category>();
        }
        // GET: api/Category
        [HttpGet]
        public IEnumerable<Category> Get()
        {
            var data = _repository.All();
            var cs = new CategoryService();
            var tree = cs.ParallelTree(ColumnList);
            return tree;
        }

        // GET: api/Category/5
        [HttpGet("{id}")]
        public string Get(int id)
        {
            return "value";
        }

        // POST: api/Category
        [HttpPost]
        public void Post([FromBody]CategoryVM vm)
        {
            var category = new Category(vm.Name, vm.Sort, vm.ParentID);
            _repository.Save(category);

            var item = _repository.Find(category.ID);
            if (!string.IsNullOrWhiteSpace(item.Name))
            {
                item.Name = "bbb";
                _repository.Save(item);
            }
        }

        // PUT: api/Category/5
        [HttpPut("{id}")]
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE: api/ApiWithActions/5
        [HttpDelete("{id}")]
        public void Delete(int id)
        {
        }

        [HttpGet("CategorySelectList")]
        public List<Category> CategorySelectList()
        {
            var cs = new CategoryService();
            return cs.ParallelTree(ColumnList);
        }
        private List<Category> ColumnList = new List<Category>
            {
                new Category{ID=Guid.Parse("{485A4F28-E661-47C2-A72A-D0FA0B777ACC}"),Name="预测学",ParentID=null,Sort=1},
                  new Category{ID=Guid.Parse("{2E79F720-D85C-4415-9DE5-D8008C55EF12}"),Name="周易预测",ParentID=Guid.Parse("{485A4F28-E661-47C2-A72A-D0FA0B777ACC}"),Sort=1},
                new Category{ID=Guid.Parse("{149D09B5-CE1F-4216-801D-C4FBC5D5B25C}"),Name="预测天气",ParentID=Guid.Parse("{2E79F720-D85C-4415-9DE5-D8008C55EF12}"),Sort=1},
                new Category{ID=Guid.Parse("{0A7713A2-9163-4783-B617-FF2719B0B229}"),Name="预测财运",ParentID=Guid.Parse("{2E79F720-D85C-4415-9DE5-D8008C55EF12}"),Sort=1},
                new Category{ID=Guid.Parse("{940D24CA-5635-4BD9-AAF2-6E8B623A45A4}"),Name="八字预测",ParentID=Guid.Parse("{485A4F28-E661-47C2-A72A-D0FA0B777ACC}"),Sort=1},
                new Category{ID=Guid.Parse("{F405E976-6974-43B8-BFCC-33D83E35A6F9}"),Name="计算机",ParentID=null,Sort=1,IsDisabled=true},
           new Category{ID=Guid.Parse("{DE2DFC46-B365-4142-8841-AA9C4A7C24A4}"),Name="硬件",ParentID=Guid.Parse("{F405E976-6974-43B8-BFCC-33D83E35A6F9}"),Sort=1},
                new Category{ID=Guid.Parse("{831AC996-1458-42B3-A6FF-36DC251FFC7F}"),Name="软件",ParentID=Guid.Parse("{F405E976-6974-43B8-BFCC-33D83E35A6F9}"),Sort=1},
                new Category{ID=Guid.Parse("{B7DF0BB0-BC43-4460-B16D-45E44DAFCD5D}"),Name="应用软件",ParentID=Guid.Parse("{831AC996-1458-42B3-A6FF-36DC251FFC7F}"),Sort=1},
                new Category{ID=Guid.Parse("{324F9275-BABC-40C3-8D9C-9FD0173C8537}"),Name="web",ParentID=Guid.Parse("{B7DF0BB0-BC43-4460-B16D-45E44DAFCD5D}"),Sort=1},
              new Category{ID=Guid.Parse("{0BD58152-FDD5-4B32-925E-CEEC03D4117E}"),Name="武术",ParentID=null,Sort=1},
                new Category{ID=Guid.Parse("{233E5105-720A-4956-B1B6-F64BAA535A85}"),Name="医学",ParentID=null,Sort=3}
            };

    }
}
